﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using UFIDA.U8.DB;
using System.Data.SqlClient;
using UFIDA.U8.WAZH.DB;
namespace UFIDA.U8.WAZH.BANKDETAIL
{
    public partial class BankDetailUI : UserControl
    {
        private UFSoft.U8.Framework.Login.UI.clsLogin login;
       
        //凭证制单人
        private string _cbill { get; set; }

        private string accid { get; set; }

        private string u8connstr { get; set; }

        public BankDetailUI(UFSoft.U8.Framework.Login.UI.clsLogin login)
        {
            // TODO: Complete member initialization
            this.login = login;
            u8connstr = login.GetLoginInfo().ConnString4Dn; //.GetDefaultConnstring(login.GetLoginInfo().AppServer);
            accid = login.GetLoginInfo().AccID;
            _cbill = login.GetLoginInfo().UserName;
            this.InitializeComponent();
            this.addAccNo(accid, u8connstr);

        }

        private void btnBatchGL_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (grvDetail.SelectedRowsCount == 0)
            {
                MessageBox.Show("请选择数据");
                return;
            }
            try
            {
                this.SendBatchGL(grvDetail.GetSelectedRows());
                this.QueryData();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }


        /// <summary>
        /// 组织凭证数据并生成凭证
        /// </summary>
        /// <param name="rows"></param>
        private void SendBatchGL(int[] rows)
        {
            int icount = 0;
            //组织凭证Entity
            List<UFIDA.U8.WAZH.DB.queryaccdetailsList> mlist = new List<DB.queryaccdetailsList>();
            foreach (var item in rows)
            {
                DataRow dr = grvDetail.GetDataRow(item);
                if (dr != null)
                {
                    if (dr["glflag"].ObjToInt32() == 1)
                    {
                        MessageBox.Show(dr["serilaId"].ObjToString() + "交易流水已生成凭证，请先删除总账凭证");
                        return;
                    }
                    mlist.Add(UFIDA.U8.WAZH.DB.DAL.cust_queryaccdetail.DataRowToModel(dr));
                }
            }
            if (mlist.Count > 0)
            {
                bool badd = this.GLAdd(accid, u8connstr, mlist,ref icount );
                MessageBox.Show("生成凭证成功,共生成" + icount + "条凭证");
                
            }
        }

        private void btnHGL_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (grvDetail.SelectedRowsCount == 0)
            {
                MessageBox.Show("请选择数据");
                return;
            }
            try
            {
                this.SendGLHB(grvDetail.GetSelectedRows());
                this.QueryData();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        /// <summary>
        /// 生成合并凭证
        /// </summary>
        /// <param name="rows"></param>
        private void SendGLHB(int[] rows)
        { 
            List<string> serialList = new List<string>();
            List<UFIDA.U8.DB.Model.GL_accvouch> vouchlist = new List<U8.DB.Model.GL_accvouch>();
            foreach (var i in rows)
            {
                string serialNo = grvDetail.GetRowCellValue(i, "serialId").ObjToString();
                int glflag = grvDetail.GetRowCellValue(i, "glflag").ObjToInt32();
                if (glflag == 1)
                {
                    MessageBox.Show(serialNo + "交易流水已生成凭证，请先删除总账凭证");
                    return;
                }
                serialList.Add(serialNo);
              
            }
            //
            if (serialList.Count > 0)
            {
              //根据收付款银行帐号生成凭证记录
                string ino_id = this.getinoid("记", login.GetLoginInfo().iYear.ObjToInt32(), login.GetLoginInfo().iMonth.ObjToInt32(), u8connstr);
                int inid = 1;
                //我方银行汇总数据
                vouchlist.AddRange(this.GetAccNoGL(serialList, ino_id.ObjToInt32(), ref inid));
                //对方银行汇总数据
                vouchlist.AddRange(this.GetOppAccNoGL(serialList, ino_id.ObjToInt32(), ref inid));

                if (vouchlist.Count > 0)
                {
                    List<UFIDA.U8.DB.CommandInfo> cmdlist = new List<CommandInfo>();
                    foreach (var item in vouchlist)
                    { 
                        cmdlist.AddRange(UFIDA.U8.DB.DAL.GL_accvouch.GetInsertCommand(item));
                    }
                    string sql = this.GetUpdGLflag(ino_id, serialList);
                    cmdlist.Add(new CommandInfo(sql, null));
                    DbHelperSQL.ExecuteSqlTran(cmdlist, u8connstr);
                    MessageBox.Show("生成凭证成功,凭证号" + ino_id);
                }
            }
        }
        /// <summary>
        /// 批量更新交易流水生成作证标志
        /// </summary>
        /// <param name="serialList"></param>
        /// <returns></returns>
        private string GetUpdGLflag(string inoid, List<string> serialList)
        {
            string sql = "update cust_queryaccdetail set glflag = 1, inoid = '"+ inoid +"' where serialId in ('"+ string.Join("','", serialList) +"')";
            return sql;
        }

        /// <summary>
        /// 根据对方账号组织凭证记录
        /// </summary>
        /// <param name="serialList"></param>
        /// <param name="ino_id"></param>
        /// <param name="inid"></param>
        /// <returns></returns>
        private List<UFIDA.U8.DB.Model.GL_accvouch> GetOppAccNoGL(List<string> serialList, int ino_id, ref int inid)
        {
            string serial = string.Join("','", serialList);
            List<UFIDA.U8.DB.Model.GL_accvouch> vlist = new List<U8.DB.Model.GL_accvouch>();
            UFIDA.U8.DB.Model.code code = null;
            StringBuilder sql = new StringBuilder();
            sql.Append(" select sum(case cdSign when 1 then 0-amt else amt end) as amt, max(transtime) as transtime, max(uses) as uses, max(oppAccName) oppAccName,(c.ccode) as oppcode, (v.cmemo) as venccode, v.cvencode ");
            sql.Append(" from cust_queryaccdetail  a ");
            sql.Append(" left join code c on c.ccode = a.oppAccNo ");
            sql.Append(" left join vendor v on v.cvenname = a.oppAccName from cust_queryaccdetail where serialId in ('" + serial + "')  group by group by c.ccode, v.cmemo, v.cvencode ");
            sql.Append(" having sum(case cdSign when 1 then 0-amt else amt end) != 0 ");

              DataTable dt = DbHelperSQL.QueryAsDT(sql.ToString(), u8connstr);
              if (dt != null && dt.Rows.Count > 0)
              {
                  foreach (DataRow dr in dt.Rows)
                  {
                      UFIDA.U8.DB.Model.GL_accvouch gl = new U8.DB.Model.GL_accvouch();
                      decimal amt = dr["amt"].ObjToDecimal();
                      DateTime transtime = dr["transtime"].ObjToDateTime();
                      string oppcode = dr["oppcode"].ObjToString();
                      string venccode = dr["venccode"].ObjToString();
                      string oppAccName = dr["oppAccName"].ObjToString();
                      string cvencode = dr["cvencode"].ObjToString();
                      string uses = dr["uses"].ObjToString();

                      gl.iperiod = transtime.Month;
                      gl.iyear = transtime.Year;
                      gl.iYPeriod = Convert.ToInt32(gl.iyear.ToString() + gl.iperiod.ToString("00"));
                      gl.ino_id = Convert.ToInt32(ino_id);
                      gl.dbill_date = transtime.ToString("yyyy-MM-dd").ObjToDateTime();
                      gl.idoc = 0;
                      gl.inid = inid;
                      gl.isignseq = 1;
                      gl.csign = "记";
                      gl.dt_date = transtime.ToString("yyyy-MM-dd").ObjToDateTime();
                       

                      gl.cbill = login.GetLoginInfo().UserName; 
                      gl.bvouchedit = true;
                      gl.bvalueedit = true;
                      gl.bcodeedit = true;
                      gl.bPCSedit = true;
                      gl.bDeptedit = true;
                      gl.bItemedit = true;
                      if (amt > 0) //金额大于0， 收款 贷方
                      {
                          //根据对方银行账号查找科目， 判断是否公司内部划转

                          if (!string.IsNullOrEmpty(oppcode))
                          { 
                              //银行科目
                              gl.ccode = code.ccode;
                              gl.cdigest = "维安内部划转";
                          }
                          else if (!string.IsNullOrEmpty(venccode))
                          {
                              //供应商工程款科目
                              gl.ccode = "11220401";
                              gl.cdigest = "收到 " + oppAccName + "工程款";
                          }
                          else
                          {
                              gl.ccode = "11220401";
                              gl.cdigest = "收到未知工程款";
                          }
                          gl.mc = amt;
                          gl.md = 0;
                          gl.md_f = gl.md;
                          gl.mc_f = gl.mc;
                      }
                      else //金额小于0 付款 
                      {
                          //供应商对应科目

                          if (!string.IsNullOrEmpty(oppcode))
                          {
                              gl.ccode = code.ccode;
                              gl.cdigest = "维安内部划转"; 
                          }
                          else if (!string.IsNullOrEmpty(venccode))
                          {
                              gl.ccode = venccode;
                              gl.csup_id = cvencode;
                              gl.cdigest = "支付 " + oppAccName + uses;
                          }
                          else
                          {
                              gl.ccode = "660298";
                              gl.cdigest = "支付 " + uses;
                          }
                          gl.mc = 0;
                          gl.md = 0-amt;
                          gl.md_f = gl.md;
                          gl.mc_f = gl.mc;
                      }
                      //code = UFIDA.U8.DB.DAL.code.GetModel(gl.ccode, gl.iyear.ObjToInt32(), u8connstr);
                      //if (code == null)
                      //{
                      //    string msg = "科目" + gl.ccode + "不在帐套" + accid + "未设置，请检查数据";
                      //    PUB.LogHelper.WriteLine(msg); 
                      //    throw new Exception(msg);
                      //}
                      inid += 1;
                      vlist.Add(gl);
                  }
              }
            return vlist;
        }

        /// <summary>
        /// 根据选择的收付流水行， 按我方银行帐号汇总收付金额 ， 金额正为借方， 金额负为贷方
        /// </summary>
        /// <param name="serialList"></param>
        /// <param name="ino_id"></param>
        /// <param name="inid"></param>
        /// <returns></returns>
        private List<UFIDA.U8.DB.Model.GL_accvouch> GetAccNoGL(List<string> serialList, int ino_id, ref int inid)
        {
            string serial = string.Join("','", serialList);
            List<UFIDA.U8.DB.Model.GL_accvouch> vlist = new List<U8.DB.Model.GL_accvouch>();
            UFIDA.U8.DB.Model.code code = null;
            StringBuilder sql = new StringBuilder();
            sql.Append("select sum(case cdSign when 1 then 0-amt else amt end) as amt, max(transtime) as transtime, bankacc from cust_queryaccdetail where serialId in ('" + serial + "')  group by bankAcc");

            DataTable dt = DbHelperSQL.QueryAsDT(sql.ToString(), u8connstr);
            if (dt!=null && dt.Rows.Count > 0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    UFIDA.U8.DB.Model.GL_accvouch gl = new U8.DB.Model.GL_accvouch();
                    decimal amt = dr["amt"].ObjToDecimal();
                    DateTime transtime = dr["transtime"].ObjToDateTime();
                    string bankAcc = dr["bankAcc"].ObjToString();
                    //string oppAccName = dr["oppAccName"].ObjToString();

                    gl.iperiod = transtime.Month;
                    gl.iyear = transtime.Year;
                    gl.iYPeriod = Convert.ToInt32(gl.iyear.ToString() + gl.iperiod.ToString("00"));
                    gl.ino_id = Convert.ToInt32(ino_id);
                    gl.dbill_date = transtime.ToString("yyyy-MM-dd").ObjToDateTime();
                    gl.idoc = 0;
                    gl.inid = inid;
                    gl.isignseq = 1;
                    gl.csign = "记";
                    gl.dt_date = transtime.ToString("yyyy-MM-dd").ObjToDateTime();
                    //根据银行确定科目 
                    code = UFIDA.U8.DB.DAL.code.GetModelbyName(bankAcc, gl.iyear.ObjToInt32(), u8connstr);

                    if (code != null)
                    {
                        gl.ccode = code.ccode;
                    }
                    else
                    {
                        //银行在帐套中不存在
                        continue;
                    } 

                    gl.cbill = login.GetLoginInfo().UserName; 
                    gl.bvouchedit = true;
                    gl.bvalueedit = true;
                    gl.bcodeedit = true;
                    gl.bPCSedit = true;
                    gl.bDeptedit = true;
                    gl.bItemedit = true;
                    if (amt > 0) //金额大于0 借方
                    { 
                        //对方科目
                        gl.ccode_equal = "11220401";
                        gl.cdigest = "收款"; 
                        gl.mc = 0;
                        gl.md = amt;
                        gl.md_f = gl.md;
                        gl.mc_f = gl.mc; 
                    }
                    else
                    { 
                        //对方科目
                        gl.ccode_equal = "11220401";
                        gl.cdigest = "付款";  
                        gl.mc = 0-amt;
                        gl.md = 0;
                        gl.md_f = gl.md;
                        gl.mc_f = gl.mc;
                    }
                    inid += 1;
                    vlist.Add(gl);
                }
            }
            return vlist;
        }

        private void btnQuery_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            this.QueryData();
        }

        private void QueryData()
        {
            string sql = "select * from cust_queryaccdetail WHERE 1=1 ";
            try
            {
                string strWhere = this.getFilter();
                if (!string.IsNullOrEmpty(strWhere))
                {
                    sql += strWhere;
                }
                sql += " order by transtime";
                DataTable dt = UFIDA.U8.DB.DbHelperSQL.QueryAsDT(sql, u8connstr);
                gridControlDetail.DataSource = dt;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void addAccNo(string accid, string connstr)
        {
            string sql = "select record from syncdb..cust_accconfig where accid = '" + accid + "'";
            DataTable dt = DbHelperSQL.QueryAsDT(sql, connstr);
            if (dt!= null && dt.Rows.Count > 0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    edtAccNo.Properties.Items.Add(dr["record"].ToString());
                }
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        private string getFilter()
        {
            StringBuilder str = new StringBuilder();
            if (!string.IsNullOrEmpty(edtBgndate.Text))
            {
                str.Append(" and convert(varchar(10), transtime,121) >= '" + edtBgndate.Text.ObjToDateTime().ToString("yyyy-MM-dd") + "'");
            }

            if (!string.IsNullOrEmpty(edtEnddate.Text))
            {
                str.Append(" and convert(varchar(10), transtime,121) <= '" + edtEnddate.Text.ObjToDateTime().ToString("yyyy-MM-dd") + "'");
            }

            if (!string.IsNullOrEmpty(edtAccNo.Text))
            {
                str.Append(" and bankAcc like '%" + edtAccNo.Text + "%'");
            }

            if (!string.IsNullOrEmpty(edtOPAccNo.Text))
            {
                str.Append(" and oppAccNo like '%" + edtOPAccNo.Text + "%'");
            }

            if (!string.IsNullOrEmpty(edtYT.Text))
            {
                str.Append(" and uses like '%" + edtYT.Text + "%'");
            }

            if (!string.IsNullOrEmpty(cbedtType.Text))
            {
                str.Append(" and cdSignName = '" + cbedtType.Text + "'");
            }
            if (!string.IsNullOrEmpty(cbedtGL.Text))
            {
                string strgl = cbedtGL.Text;
                if (strgl.Equals("否"))
                {
                    str.Append(" and glflag = 0 ");
                }
                else if (strgl.Equals("是"))
                {
                    str.Append(" and glflag = 1 ");
                }
                
            }

            //过滤掉收款中内部转移的部分
            str.Append(" and not exists (select 1 from syncdb..cust_Accconfig where cust_Accconfig.accid=" + login.GetLoginInfo().AccID + " and cust_Accconfig.record = cust_queryaccdetail.oppaccno and cust_queryaccdetail.cdSign = 0)");
            return str.ToString();
        }
         
        private bool AccNoExists(string Accno, string accid, string u8connstr)
        {
            string sql = "select 1 from syncdb..cust_Accconfig where accid = '" + accid + "' and record = '" + Accno + "'";
            return DbHelperSQL.Exists(sql, u8connstr);
        }


        /// <summary>
        /// 获取凭证类别 最大凭证号
        /// </summary>
        /// <param name="csign"></param>
        /// <param name="iyear"></param>
        /// <param name="imonth"></param>
        /// <param name="u8connstr"></param>
        /// <returns></returns>
        private string getinoid(string csign, int iyear, int imonth, string u8connstr)
        {
            string sino = string.Empty;
            string sql = "set tran isolation level read uncommitted; select isnull(MAX(ino_id), 0) +1 as ino_id  from GL_accvouch with(nolock) where iperiod = " + imonth + " and iyear = " + iyear +
                " and csign = '" + csign + "'";
            using (SqlConnection conn = new SqlConnection(u8connstr))
            {
                using (SqlCommand cmd = new SqlCommand(sql, conn))
                {
                    try
                    {
                        conn.Open();
                        DataTable dt = new DataTable();
                        SqlDataAdapter da = new SqlDataAdapter(cmd);
                        da.Fill(dt);
                        if (dt != null && dt.Rows.Count > 0)
                        {
                            sino = dt.Rows[0]["ino_id"].ToString();
                        }
                    }
                    finally
                    {
                        conn.Close();
                    }
                }
            }
            return sino;
        }
        /// <summary>
        /// 插入凭证
        /// </summary>
        /// <param name="accid"></param>
        /// <param name="u8connstr"></param>
        /// <returns></returns>
        private bool GLAdd(string accid, string u8connstr, List<queryaccdetailsList>  mlist, ref int icount)
        {
            bool bresult = true;
            UFIDA.U8.DB.Model.code code = null;
            try
            {
                //查找未生成凭证的记录
                //List<queryaccdetailsList> mlist = UFIDA.U8.WAZH.DB.DAL.cust_queryaccdetail.GetModelWhere(" and glflag = 0", u8connstr);
                if (mlist != null)
                { 
                    foreach (var item in mlist)
                    {
                        List<UFIDA.U8.DB.CommandInfo> cmdlist = new List<CommandInfo>();
                        UFIDA.U8.DB.Model.GL_accvouch jf = new U8.DB.Model.GL_accvouch();
                        UFIDA.U8.DB.Model.GL_accvouch df = new U8.DB.Model.GL_accvouch();

                        string ino_id = this.getinoid("记", item.transtime.ObjToDateTime().Year, item.transtime.ObjToDateTime().Month, u8connstr);

                        #region 组织凭证
                        if (item.cdSign == "0") //收款
                        {
                            //借方
                            jf.iperiod = item.transtime.ObjToDateTime().Month;
                            jf.iyear = item.transtime.ObjToDateTime().Year;
                            jf.iYPeriod = Convert.ToInt32(jf.iyear.ToString() + jf.iperiod.ToString("00"));
                            jf.ino_id = Convert.ToInt32(ino_id);
                            jf.dbill_date = item.transtime.ObjToDateTime().ToString("yyyy-MM-dd").ObjToDateTime();
                            jf.idoc = 0;
                            jf.inid = 1;
                            jf.isignseq = 1;
                            jf.csign = "记";
                            jf.dt_date = item.transtime.ObjToDateTime().ToString("yyyy-MM-dd").ObjToDateTime();
                            //客户辅助
                            code = UFIDA.U8.DB.DAL.code.GetModelbyName(item.bankAcc, jf.iyear.ObjToInt32(), u8connstr);

                            if (code != null)
                            {
                                jf.ccode = code.ccode;
                            }
                            else
                            {
                                continue;
                            }
                            code = UFIDA.U8.DB.DAL.code.GetModelbyName(item.oppAccNo, jf.iyear.ObjToInt32(), u8connstr);
                            if (code != null)
                            {
                                if (this.AccNoExists(item.oppAccNo, accid, u8connstr))
                                {
                                    //内部划转的支付记录不再生成凭证
                                    UFIDA.U8.WAZH.DB.DAL.cust_queryaccdetail.UpdGLFlagCommand(item.serialId, u8connstr);
                                    continue;
                                }
                                //对方科目
                                jf.ccode_equal = code.ccode;
                                jf.cdigest = "维安内部划转";
                            }
                            else
                            {
                                //对方科目
                                jf.ccode_equal = "11220401";
                                jf.cdigest = "收到 " + item.oppAccName + "工程款";
                            }

                            jf.mc = 0;
                            jf.md = item.amt.ObjToDecimal();
                            jf.md_f = jf.md;
                            jf.mc_f = jf.mc;

                            jf.cbill = _cbill;
                            jf.coutid = "NB" + item.serialId;
                            jf.bvouchedit = true;
                            jf.bvalueedit = true;
                            jf.bcodeedit = true;
                            jf.bPCSedit = true;
                            jf.bDeptedit = true;
                            jf.bItemedit = true;

                            //贷方
                            df.iperiod = jf.iperiod;
                            df.iyear = jf.iyear;
                            df.iYPeriod = jf.iYPeriod;
                            df.ino_id = jf.ino_id;
                            df.dbill_date = jf.dbill_date;
                            df.idoc = 0;
                            df.inid = 2;
                            df.isignseq = jf.isignseq;
                            df.csign = jf.csign;

                            df.ccode = jf.ccode_equal;
                            code = UFIDA.U8.DB.DAL.code.GetModel(df.ccode, df.iyear.ObjToInt32(), u8connstr);
                            if (code != null && code.bcus)
                            {
                                UFIDA.U8.DB.Model.Customer cus = UFIDA.U8.DB.DAL.Customer.GetModelByName(item.oppAccName, u8connstr);
                                if (cus != null)
                                {
                                    df.ccus_id = cus.cCusCode;
                                }
                            }
                            //对方科目
                            df.ccode_equal = jf.ccode;
                            df.mc = jf.md;
                            df.md = jf.mc;
                            df.md_f = df.md;
                            df.mc_f = df.mc;
                            df.dt_date = jf.dt_date;
                            df.nfrat = jf.nfrat;

                            df.cdigest = jf.cdigest;
                            df.cexch_name = jf.cexch_name;

                            df.cbill = jf.cbill;
                            df.coutid = jf.coutid;


                            df.bvouchedit = true;
                            df.bvalueedit = true;
                            df.bcodeedit = true;
                            df.bPCSedit = true;
                            df.bDeptedit = true;
                            df.bItemedit = true;
                        }
                        else if (item.cdSign == "1") //付款
                        {
                            //借方
                            jf.iperiod = item.transtime.ObjToDateTime().Month;
                            jf.iyear = item.transtime.ObjToDateTime().Year;
                            jf.iYPeriod = Convert.ToInt32(jf.iyear.ToString() + jf.iperiod.ToString("00"));
                            jf.ino_id = Convert.ToInt32(ino_id);
                            jf.dbill_date = item.transtime.ObjToDate().ObjToDateTime();
                            jf.idoc = 0;
                            jf.inid = 1;
                            jf.isignseq = 1;
                            jf.csign = "记";
                            jf.dt_date = item.transtime.ObjToDateTime().ToString("yyyy-MM-dd").ObjToDateTime();

                            UFIDA.U8.DB.Model.Vendor ven = UFIDA.U8.DB.DAL.Vendor.GetModelWhere(" and cvenname = '" + item.oppAccName + "'", u8connstr);

                            if (ven == null || string.IsNullOrEmpty(ven.cMemo))
                            {

                                code = UFIDA.U8.DB.DAL.code.GetModelbyName(item.oppAccNo, jf.iyear.ObjToInt32(), u8connstr);
                                if (code != null)
                                {
                                    jf.ccode = code.ccode;
                                    jf.cdigest = "维安内部划转";
                                }
                                else
                                {
                                    jf.ccode = "660298";
                                    jf.cdigest = "支付 " + item.oppAccName + item.uses;
                                }
                            }
                            else
                            {
                                jf.ccode = ven.cMemo;
                                jf.csup_id = ven.cVenCode;
                                jf.cdigest = "支付 " + item.oppAccName + item.uses;
                            }

                            code = UFIDA.U8.DB.DAL.code.GetModel(jf.ccode, jf.iyear.ObjToInt32(), u8connstr);
                            if (code == null)
                            {
                                PUB.LogHelper.WriteLine("科目" + jf.ccode + "不在帐套" + accid + "未设置，请检查数据");
                                continue;
                            }

                            code = UFIDA.U8.DB.DAL.code.GetModelbyName(item.bankAcc, jf.iyear.ObjToInt32(), u8connstr);
                            if (code != null)
                            {
                                jf.ccode_equal = code.ccode;
                            }
                            else
                            {
                                PUB.LogHelper.WriteLine("贷方银行科目" + item.bankAcc + "在科目表中不存在");
                                continue;
                            }

                            jf.mc = 0;
                            jf.md = item.amt.ObjToDecimal();
                            jf.md_f = jf.md;
                            jf.mc_f = jf.mc;
                            //jf.nfrat = 1;

                            //jf.cexch_name = "人民币";
                            jf.cbill = _cbill;
                            jf.coutid = "NB" + item.serialId;

                            jf.bvouchedit = true;
                            jf.bvalueedit = true;
                            jf.bcodeedit = true;
                            jf.bPCSedit = true;
                            jf.bDeptedit = true;
                            jf.bItemedit = true;
                            //贷方
                            df.iperiod = jf.iperiod;
                            df.iyear = jf.iyear;
                            df.iYPeriod = jf.iYPeriod;
                            df.ino_id = jf.ino_id;
                            df.dbill_date = jf.dbill_date;
                            df.idoc = 0;
                            df.inid = 2;
                            df.isignseq = jf.isignseq;
                            df.csign = jf.csign;
                            df.dt_date = jf.dt_date;

                            df.ccode = jf.ccode_equal;

                            //对方科目
                            df.ccode_equal = jf.ccode;
                            df.mc = jf.md;
                            df.md = jf.mc;
                            df.md_f = df.md;
                            df.mc_f = df.mc;

                            df.nfrat = jf.nfrat;

                            df.cdigest = jf.cdigest;
                            df.cexch_name = jf.cexch_name;

                            df.cbill = jf.cbill;
                            df.coutid = jf.coutid;

                            df.bvouchedit = true;
                            df.bvalueedit = true;
                            df.bcodeedit = true;
                            df.bPCSedit = true;
                            df.bDeptedit = true;
                            df.bItemedit = true;
                        }
                        icount += 1;
                        cmdlist.AddRange(UFIDA.U8.DB.DAL.GL_accvouch.GetInsertCommand(jf));
                        cmdlist.AddRange(UFIDA.U8.DB.DAL.GL_accvouch.GetInsertCommand(df));

                        //更新下载成功标志
                        cmdlist.Add(UFIDA.U8.WAZH.DB.DAL.cust_queryaccdetail.GetUpdGLFlagCommand(item.serialId, ino_id));
                        if (cmdlist.Count > 0)
                        {
                            UFIDA.U8.DB.DbHelperSQL.ExecuteSqlTran(cmdlist, u8connstr);
                            PUB.LogHelper.WriteLine("生成凭证" + ino_id);
                        }

                        #endregion
                    }

                    PUB.LogHelper.WriteLine("生成凭证完成，共生成" + icount + "张凭证");
                }
            }
            catch (Exception e)
            {
                throw e;
            }

            return bresult;
        }

        private void grvDetail_SelectionChanged(object sender, DevExpress.Data.SelectionChangedEventArgs e)
        {
            //
            MessageBox.Show(e.Action.ToString());
        }
    }
}
